#ifndef _AT7456E_H
#define _AT7456E_H

#include "FreeRTOS.h"
#include "task.h"
#include "spi2.h"
#include "font.h"
#include "gps.h"
#include "button.h"
#include "Wifi.h"
#include "usart4.h"

// at7456
#define VM0 0x00 // Video Mode0
#define VM1 0x01 // Video Mode1
#define HOS 0x02 // Horizontal Offset
#define VOS 0x03 // Vertical Offset
#define DMM 0x04 // Display Memory Mode
#define DMAH 0x05 // Display Memory Address High
#define DMAL 0x06 // Display Memory Address Low
#define DMDI 0x07 // Display Memory Data In
#define CMM 0x08 // Character Memory Mode
#define CMAH 0x09 // Character Memory Address High
#define CMAL 0x0a // Character Memory Address Low
#define CMDI 0x0b // Character Memory Data In
#define OSDM 0x0c // OSD Insertion Mux
#define RB0 0x10 // Row 0 Brightness
#define RB1 0x11 // Row 1 Brightness
#define RB2 0x12 // Row 2 Brightness
#define RB3 0x13 // Row 3 Brightness
#define RB4 0x14 // Row 4 Brightness
#define RB5 0x15 // Row 5 Brightness
#define RB6 0x16 // Row 6 Brightness
#define RB7 0x17 // Row 7 Brightness
#define RB8 0x18 // Row 8 Brightness
#define RB9 0x19 // Row 9 Brightness
#define RB10 0x1a // Row 10 Brightness
#define RB11 0x1b // Row 11 Brightness
#define RB12 0x1c // Row 12 Brightness
#define RB13 0x1d // Row 13 Brightness
#define RB14 0x1e // Row 14 Brightness
#define RB15 0x1f // Row 15 Brightness
#define OSDBL 0x6c // OSD Black Level
#define STAT 0x20 // Status
#define DMDO 0x30 // Display Memory Data Out
#define CMDO 0x40 // Character Memory Data Out
#define NVM_RAM 0x50 // 将 NVM 中的字库读取到镜像 RAM 中
#define RAM_NVM 0xa0 // 将镜像 RAM 中的字库数据写到 NVM 中
// VM0
#define NTSC (0 << 6) // D6 --- Vidoe Standard Select
#define PAL (1 << 6)
#define SYNC_AUTO (0 << 4) // D5,D4 --- Sync Select Mode
#define SYNC_EXTERNAL (2 << 4)
#define SYNC_INTERNAL (3 << 4)
#define OSD_ENABLE (1 << 3) // D3 --- Enable Display of OSD image
#define OSD_DISABLE (0 << 3)
#define SOFT_RESET (1 << 1) // D1 --- Software Reset
#define VOUT_ENABLE (0 << 0)
#define VOUT_DISABLE (1 << 0)
// VM1
#define BACKGND_0 (0 << 4) // 背景电平 WHT%
#define BACKGND_7 (1 << 4)
#define BACKGND_14 (2 << 4)
#define BACKGND_21 (3 << 4)
#define BACKGND_28 (4 << 4)
#define BACKGND_35 (5 << 4)
#define BACKGND_42 (6 << 4)
#define BACKGND_49 (7 << 4)
#define BLINK_TIME40 (0 << 2) // 闪烁周期, ms
#define BLINK_TIME80 (1 << 2)
#define BLINK_TIME120 (2 << 2)
#define BLINK_TIME160 (3 << 2)
// 闪烁占空比(ON : OFF)
#define BLINK_DUTY_1_1 0 // BT : BT
#define BLINK_DUTY_1_2 1 // BT : 2BT
#define BLINK_DUTY_1_3 2 // BT : 3BT
#define BLINK_DUTY_3_1 3 // 3BT : BT
// DMM
#define SPI_BIT16 (0 << 6) // 写字符时采用 16bit 方式，字符属性来自 DMM[5:3]
#define SPI_BIT8 (1 << 6) // 写字符时用 8bit 方式，字符属性单独写入
#define CHAR_LBC (1 << 5) // 本地背景
#define CHAR_BLK (1 << 4) // 闪烁显示
#define CHAR_INV (1 << 3) // 负像显示
#define CLEAR_SRAM (1 << 2) // 清屏， 20us
#define VETICAL_SYNC (1 << 1) // 命令在场同步后起作用
#define AUTO_INC (1 << 0) // 字符地址自动递增
// RBi
#define BLACK_LEVEL_0 (0 << 2) // 0% 白电平
#define BLACK_LEVEL_10 (1 << 2) // 10% 白电平
#define BLACK_LEVEL_20 (2 << 2) // 20% 白电平
#define BLACK_LEVEL_30 (3 << 2) // 30% 白电平
#define WHITE_LEVEL_120 (0 << 0) // 120% 白电平
#define WHITE_LEVEL_100 (1 << 0) // 110% 白电平
#define WHITE_LEVEL_90 (2 << 0) // 90% 白电平
#define WHITE_LEVEL_80 (3 << 0) // 80% 白电平
// STAT
#define PAL_DETECT (1 << 0) // 检测到 PAL 信号
#define NTSC_DETECT (1 << 1) // 检测到 NTSC 信号
#define LOS_DETECT (1 << 2) // 检测到 LOS 信号
#define VSYNC (1 << 4) // 场同步

#define BatterySample_MAXNUM 60


#define zoom_x1 	0x0000 
#define zoom_x2 	0x1797
#define zoom_x3 	0x2384
#define zoom_x4 	0x2B27
#define zoom_x5 	0x30A4
#define zoom_x6 	0x34D8
#define zoom_x7 	0x385D
#define zoom_x8 	0x3B32
#define zoom_x9 	0x3DC4
#define zoom_x10 	0x4000
#define zoom_x11 	0x6000
#define zoom_x12 	0x6A80
#define zoom_x13 	0x7000
#define zoom_x14 	0x7300
#define zoom_x15 	0x7540  


#define zoom_x16 	0x76C0  

#define zoom_x17 	0x7800 
#define zoom_x18 	0x78C0 
#define zoom_x19 	0x7980 
#define zoom_x20 	0x7A00 
#define zoom_x21 	0x7AC0
#define zoom_x22 	0x7B40 
#define zoom_x23 	0x7B80 
#define zoom_x24 	0x7BC0 
#define zoom_x25 	0x7C00 
#define zoom_x26 	0x7C40
#define zoom_x27 	0x7C80
#define zoom_x28 	0x7CC0 
#define zoom_x29 	0x7D00
#define zoom_x30 	0x7D40 
#define zoom_x31    0x7D80 
#define zoom_x32    0x7DC0
#define zoom_x33    0x7E00 
//0x7540


#define DefaultZoom 			0x01

#define Default_VL_Brightness	  1
#define Default_VL_Contract		  1
#define Default_VL_Mode           1
#define Default_VL_DWDR           1
#define Default_VL_DNR            1
#define Default_VL_PR             1
#define Default_VL_Wifi           1
								 
#define Default_IR_Brightness	  1
#define Default_IR_Contract       1
#define Default_IR_Enhance        1
#define Default_IR_DNR            1
#define Default_IR_Polar          1
#define Default_IR_PR             1
#define Default_IR_Wifi           1



extern unsigned char RecOn_Cmd[]; //录像
extern unsigned char RecOff_Cmd[]; //录像关
extern unsigned char Wifyon_Cmd[]; //WIFY开
extern unsigned char Wifyoff_Cmd[];//WIFY关-

//VL
extern unsigned char VL_Reboot_Cmd[]; //重启
extern unsigned char VL_Bright_Cmd[];//亮度
extern unsigned char VL_Contrast_Cmd[];//对比度

extern unsigned char VL_ColorOn_Cmd[];//彩色模式开
extern unsigned char VL_Mono_Cmd[];//黑白模式开
extern unsigned char VL_DefogOn_Cmd[];//透雾模式开

extern unsigned char Compass_state;


typedef enum
{
OLD7456,
NEW7456,
BAD7456
} VER7456;
typedef enum
{
MODE_PAL,
MODE_NTSC,
MODE_ERR
} VIDEO_MODE;
typedef enum
{
	_Percent0 = 0,
	_Percent10,
	_Percent20,
	_Percent30,
	_Percent40,
	_Percent50,
	_Percent60,
	_Percent70,
	_Percent80,
	_Percent90,
	_Percent100
}_BatteryState;
extern _BatteryState BatteryState;

typedef struct
{
unsigned short index; // 字库索引： AT7456 = {0..255}； AT7456E = {0..511}
unsigned char attrib; // 显示属性： D2 - LBC； D1 - BLK； D0 - INV
}CHAR_ATTRIB;


//IR
#if ChengDu 
extern unsigned char IR_Polar_WHot_Cmd[] 			;
extern unsigned char IR_Polar_BHot_Cmd[] 			;
extern unsigned char IR_Polar_RainBow_Cmd[] 	;
extern unsigned char IR_Polar_Green_Cmd[] 		;
extern unsigned char IR_Polar_Iron_Cmd[] 			;
																							
extern unsigned char IR_Bright_Cmd[] 					;
extern unsigned char IR_Contract_Cmd[] 				;
extern unsigned char IR_DDE_Cmd[] 						;
extern unsigned char IR_DNR_Cmd[] 						;
extern unsigned char IR_ZOOM_Cmd[] 						;
extern unsigned char IR_GEAR_Cmd[]						;

//PAL隔行
extern unsigned char IR_BT656_PAL1_Cmd[]							;
//PAL逐行							
extern unsigned char IR_BT656_PAL2_Cmd[]							;
extern unsigned char IR_BT656_StripeDenoising_Cmd[]			;
#elif BT656
extern unsigned char IR_Polar_WHot_Cmd[] 			;
extern unsigned char IR_Polar_BHot_Cmd[] 			;
extern unsigned char IR_Polar_Green_Cmd[] 		    ;
extern unsigned char IR_Polar_Iron_Cmd[] 			;
																							
extern unsigned char IR_Bright_Cmd[] 			    ;
extern unsigned char IR_Contract_Cmd[] 				;
extern unsigned char IR_DDE_Cmd[] 					;

extern unsigned char IR_OutDoor_ON_Cmd[] 			;
extern unsigned char IR_OutDoor_OFF_Cmd[] 			;

extern unsigned char IR_Hot_Point_ON_Cmd[]          ;
extern unsigned char IR_Hot_Point_OFF_Cmd[]         ;

extern unsigned char IR_ZOOM_Cmd[] 					;

//PAL隔行
extern unsigned char IR_BT656_PAL1_Cmd[]							;
//PAL逐行							
extern unsigned char IR_BT656_PAL2_Cmd[]							;
extern unsigned char IR_BT656_StripeDenoising_Cmd[]			;

#endif

			 
//VL	 	
extern unsigned char VL_Reboot_Cmd[] 					;//重启
																							
extern unsigned char VL_Mode_Auto_Cmd[] 			;//
extern unsigned char VL_Mode_Day_Cmd[] 				;//彩色模式
extern unsigned char VL_Mode_Night_Cmd[] 			;//黑白模式

extern unsigned char VL_Mode_Defog_ON_Cmd[] 	;
extern unsigned char VL_Mode_Defog_OFF_Cmd[] 	;
extern unsigned char VL_Mode_Defog_Low_Cmd[] 	;
extern unsigned char VL_Mode_Defog_Mid_Cmd[] 	;
extern unsigned char VL_Mode_Defog_Hig_Cmd[] 	;
			 
extern unsigned char VL_Bright_Cmd[]   				;
extern unsigned char VL_Contract_Cmd[]   			;
																							
extern unsigned char VL_DWDR_Off_Cmd[]   			;
extern unsigned char VL_DWDR_On_Cmd[]   			;
extern unsigned char VL_DWDR_Low_Cmd[]   			;
extern unsigned char VL_DWDR_Mid_Cmd[]   			;
extern unsigned char VL_DWDR_Hig_Cmd []   			;

//缩放
extern unsigned char VL_ZoomStop_Cmd[];
extern unsigned char VL_ZoomSet_Cmd[];

extern unsigned char VL_ZoomON_Cmd[]				;
extern unsigned char VL_ZoomOFF_Cmd[]				;
extern unsigned char VL_Zoom_SepatateMode_Cmd[]		;
extern unsigned char VL_Zoom_CombineMode_Cmd[]		;
extern unsigned char VL_DZoom_PresetOFF_Cmd[]				;

extern unsigned char VL_ZoomSet_Cmd[];

//降噪
extern unsigned char VL_DNR_Cmd[]   				;
																						
//共有                                      	
//wifi                                      	
extern unsigned char IR_VL_WiFi_ON_Cmd[]   		;
extern unsigned char IR_VL_WiFi_OFF_Cmd[]   	;
//录像                                      	
extern unsigned char IR_VL_DVR_ON_Cmd[]   		;
extern unsigned char IR_VL_DVR_OFF_Cmd[]   		;

extern unsigned char WifiTakephoto_Cmd[]			; 	//拍一张照片

extern unsigned char Take_Photo_1718_Cmd[]			    ; 	//拍一张照片
extern unsigned char Start_Stop_Recording_1718_Cmd[]	; 	//拍一张照片
extern unsigned char Stop_Recording_1718_Cmd[]			; 	//拍一张照片







void BT656_Init(void);

void DisplayChar(unsigned char row, unsigned char col, unsigned char c);
void DisplayNum(unsigned char row, unsigned char col, unsigned char c);
void DisplayString(unsigned char row, unsigned char col, unsigned char *s);
void at7456_reset(void);
void turnOff_osd(void);
void turnOn_osd(void);
void clear_screen(void);
VER7456 checkAT7456(void);
void configOSDBL(unsigned char autoLevelCtrl);
void write_at7456_font(unsigned char addr, unsigned char *dt);
void read_at7456_font(unsigned char addr, unsigned char *dt);
void wait_memory_wr_ok(void);
void write_at7456E_font(unsigned short addr, unsigned char *dt);
void read_at7456E_font(unsigned short addr, unsigned char *dt);
void at7456_read_char(unsigned char row, unsigned char col, CHAR_ATTRIB *ca);
void at7456_write_char(unsigned char row, unsigned char col, CHAR_ATTRIB *ca);
void at7456E_read_char(unsigned char row, unsigned char col, CHAR_ATTRIB *ca);
void at7456E_write_char(unsigned char row, unsigned char col, CHAR_ATTRIB *ca);
void AT7654_Int(void);
void WriteFont_Digit(unsigned char **data);
void WriteFont_Char(unsigned char **Uppercase, unsigned char **lowercase);

void OsdDisplay_Module(uint8_t *local_xy, unsigned char *DataAddr_Buff, unsigned char  AddrSize,	uint8_t Property);

void CheckOsdReresh(OsdRefresh_Struct *_OsdRefresh_Family);
void Init_OsdRefresh_Family(OsdRefresh_Struct *_OsdRefresh_Family);

unsigned char MyStrlen(unsigned char *arry);
unsigned char MyArrayCopy(unsigned char *src,unsigned char *dex);
unsigned char MyArrayCopy_LEN(unsigned char *src,unsigned char *dex,uint8_t len);

void OsdDisplay_Module(uint8_t *local_xy, unsigned char *DataAddr_Buff, unsigned char  AddrSize, uint8_t Property);
void DaylightOsdShow(void);
void InfraredOsdShow(void);
void ConfigMenu_OsdShowON(void);
void CursorMove_OsdShow(void);
uint8_t StrTODec(uint8_t *Str_Data);
void ConfigMenu_OsdShowOFF(void);
void CursorReset_OsdShow(void);
int BatteryShow(void);
void WriteFont_MyEnglishStr(unsigned char **data,uint8_t size,uint8_t Startaddr);
void WriteFont_MyChineseStr(unsigned char **data,uint8_t size,uint8_t Startaddr);
//写图标字库
void WriteFont_Icon(unsigned char **data,uint8_t size);
void WriteFontAll(unsigned char *data, uint8_t FontSize);

void ConfigMenu_VL_OsdShowON(void);
void ConfigMenu_IR_OsdShowON(void);

void Daylight_AmplifierReduce_OsdShow(uint8_t Mode);
void Infrared_AmplifierReduce_OsdShow(uint8_t Mode);

void SetVLmode(void);
void SetIRmode(void);
void Init_PowerIO(void);

void VL_ZoomStop(void);
void VL_DZoomON(void);
void VL_DZoomOFF(void);
void VL_DZoomStop(void);
void VL_DZoomCombine(void);
void VL_DZoomSeparate(void);
void VL_Zoom_DZoomSet(uint8_t data);
//使激光图标闪烁
void LaserIconBlink(void);
void VLRestart_Reset(void);

//切白光把所有白光要展示的模块清遍
void EraseInfraredOsdShow(void);

//切红外把所有白光要展示的模块清遍
void EraseDaylightOsdShow(void);
/**
 * @brief  bsp debug
 * @param  none
 * @retval none
 */
void VL_IR_Driver(void);



//红外亮度和校验
void IR_SumCheck(uint8_t *buf);
void IR_ZoomMCheck(uint8_t *buf);
void IR_ZoomShow(uint8_t data);
void ShowLoading(void);
void EraseLoading(void);

void ShowCompassCorrection(void);
void EraseCompassCorrection(void);



#endif

